#include "configclass.h"


#ifdef _TTY_POSIX_
#include <sys/time.h>


int setLinuxSysDataTime(int year, int mon, int mday, int hour, int min, int sec)
{
//    QString timeStr = QObject::tr("data -s %1-%2-%3 %1:%2:%3").arg(year).arg(mon).arg(mday)
//            .arg(hour).arg(min).arg(sec);

    int ret;
    time_t timeT;
    struct ::tm nowTime;
    struct timeval tv;

    nowTime.tm_sec = sec;
    nowTime.tm_min = min;
    nowTime.tm_hour = hour;
    nowTime.tm_mday = mday;
    nowTime.tm_mon = mon-1;
    nowTime.tm_year = year - 1900;
    nowTime.tm_isdst = 0;
    timeT = mktime(&nowTime);

    tv.tv_sec = timeT;
    tv.tv_usec = 0;

//    QDateTime dateTime;

    QProcess st;
    QStringList cmdList;
    QString timeStr = QObject::tr("%1-%2-%3 %4:%5:%6").arg(year).arg(mon).arg(mday)
            .arg(hour).arg(min).arg(sec);

    cmdList << "-s" << timeStr;
    st.start("date", cmdList);
    ret = system("hwclock -w");
    ret = system("hwclock -s");
    return ret;
}
#endif


Configclass::Configclass()
{
    init();
//    setDefaultConfigValue();
    //
    this->setSlcdVersion(SLCD_VERSION_STR);
}

/*******************************************
  功能：初始化相关的信息
  ******************************************/
void Configclass::init()
{

#ifdef CEPIDS
    QString settingPath = g_AppPath + "/config.ini";
#else
    QString settingPath = g_installPath + "/config.ini";
#endif

    settings = new QSettings(settingPath, QSettings::IniFormat);

    QFile file(settingPath);

    if (!file.exists())
    {
        if (this->restoreDefaultConfig() < 0)
        {
            this->setDefaultConfigValue();
        }
    }

}

//Configclass::~Configclass()
//{

//}

/*******************************************
  功能：设置默认配置信息

//    可设置一下内容：对比度
//    1）车厢编号配置、当前位置信息（与开门侧对应）
//    2）系统时钟
//    3）亮度对比度颜色控制
//    4）故障报警配置
  ******************************************/
void Configclass::setDefaultConfigValue()
{

    QDateTime dateTime;

//    QString currentDate = dateTime.currentDateTime().toString("yyyy.MM.dd");
//    QString currentTime = dateTime.currentDateTime().toString("h:m:s");

    settings->setValue("SYSTEM/SLCD_Version", SLCD_VERSION_STR);  //SLCD权限

    settings->setValue("SYSTEM/IsMasterDev", "1");  //是否主设备，主设备有连接感光板，从设备没有，只能从主设备获取
    settings->setValue("SYSTEM/com", "ttyS0");  //是否主设备，主设备有连接感光板，从设备没有，只能从主设备获取
    settings->setValue("SYSTEM/baud", "9600");


    settings->setValue("SYSTEM/font", "Monospace");

    settings->setValue("SYSTEM/UpdatePort",         "6667");
    //系统绑定端口
    settings->setValue("SYSTEM/TcpPort", "6666");

    //日记保留天数
    settings->setValue("SYSTEM/LogReserveDays", "30");

    //是否将SLCD的状态上报
    settings->setValue("SYSTEM/ReportSlcdState", "1");

    //播放本地信息时间
    //格式比如：
    //IP:
    //日期：
    //时间：
    //car No:
    //position:Left/ Right side
    settings->setValue("SYSTEM/BroadcastInfoTime", "3");
    settings->setValue("SYSTEM/ScreenIndex", 1);

    settings->setValue("SCHEDULE/project", "");
    settings->setValue("SCHEDULE/version", "");
    settings->setValue("SCHEDULE/size", "");

    //车厢号
    settings->setValue("POSITION/car", "1");
    //当前位置(A为右侧，B为左侧)
    settings->setValue("POSITION/side", "R");

    //设置滚动文字的滚动速度
    settings->setValue("ScrollTxt/ScrollSpeed", "80");

#ifdef CEPIDS
    settings->setValue("SYSTEM/AckCePort", 7755);
    settings->setValue("SYSTEM/ReceCEPort", 7756);
#else
    settings->setValue("TISCOM/com", "ttyS3");
    settings->setValue("TISCOM/baud", "9600");
//    settings->setValue("TISCOM/fsm", "40,50");


    settings->setValue("PhotoSensor/com", "ttyS1");
    settings->setValue("PhotoSensor/baud", "19200");

    //是否收集环境光度
    settings->setValue("PhotoSensor/IsCollectPS", "1");


    //LCD ID号
    settings->setValue("SLCD/SLCD1", "1:1");
    settings->setValue("SLCD/SLCD2", "2:1");
    settings->setValue("SLCD/SLCD3", "3:1");
    settings->setValue("SLCD/SLCD4", "4:1");
    settings->setValue("SLCD/SLCD5", "5:1");
    settings->setValue("SLCD/SLCD6", "0:0");

    //SLCD串口号
    settings->setValue("SLCD/com", "ttyS4");
    settings->setValue("SLCD/baud", "19200");

    //亮度
    settings->setValue("SLCD/DefaultLV", "3");
    settings->setValue("SLCD/level1", "1-20:10");
    settings->setValue("SLCD/level2", "21-40:15");
    settings->setValue("SLCD/level3", "41-60:22");
    settings->setValue("SLCD/level4", "61-80:31");

    //CPU安全界限
    settings->setValue("SAFE/CpuLimmit", "95");
    //内存安全界限
    settings->setValue("SAFE/MemoryLimmit", "95");
    //硬盘安全界限
    settings->setValue("SAFE/HardDiskLimmit", "90");
    //安全界限
    settings->setValue("SAFE/limmit", "95");
#endif

}

/*******************************************
    功能：将当前配置保存成默认配置
  返回值: -1,失败,无现默认配置文件;-2，拷贝失败
  ******************************************/
qint8 Configclass::saveAsDefaultconfig()
{
    qint8 state = -1;
#ifdef CEPIDS
    QString defaultPath = QObject::tr("%1/%2").arg(g_AppPath).arg(DEFAULTCONFIGPATH);
    QString settingPath = g_AppPath + "/config.ini";
#else
    QString defaultPath = QObject::tr("%1/%2").arg(g_installPath).arg(DEFAULTCONFIGPATH);
    QString settingPath = g_installPath + "/config.ini";
#endif

    QDir dir;
    if (!dir.exists(defaultPath))
    {
        dir.mkpath(defaultPath);
    }

    defaultPath = defaultPath + "/config.ini";

    QFile file(settingPath);
    if (file.exists())
    {
        file.remove(defaultPath);
        bool isSuccess = file.copy(settingPath, defaultPath);
        state = 1;
        if (!isSuccess)
        {
            state = -2;
        }
        return state;
    } else {
        return state;
    }
}

/*******************************************
    功能：将配置恢复成默认配置
  返回值: -1,无默认配置文件；-2,拷贝文件失败
  ******************************************/
qint8 Configclass::restoreDefaultConfig()
{
    qint8 state = -1;

#ifdef CEPIDS
    QString defaultPath = QObject::tr("%1/%2").arg(g_AppPath).arg(DEFAULTCONFIGPATH);
    QString settingPath = g_AppPath + "/config.ini";
#else
    QString defaultPath = QObject::tr("%1/%2/config.ini").arg(g_installPath).arg(DEFAULTCONFIGPATH);
    QString settingPath = g_installPath + "/config.ini";
#endif

//    qDebug() << "defaultPath===========>" << defaultPath;

    QFile file(defaultPath);
    if (file.exists(defaultPath))
    {
        state = 1;
        ConfigStruct configStruct = getConfigStruct();
        file.remove(settingPath);

        bool isSuccess = file.copy(defaultPath, settingPath);

        if (isSuccess)
        {
            QString scheduleVersion = this->getScheduleVersion();
            int isMasterDev = this->IsMasterDev();
            this->setScheduleProName(configStruct.versionInfo.schedulePro);
            this->setResourceSize(configStruct.versionInfo.resourceSize);
            this->setSlcdVersion(SLCD_VERSION_STR);
            this->setSlcdVersion(SLCD_VERSION_STR);
            this->setMasterDev(isMasterDev);
            this->setScheduleVersion(scheduleVersion);

            settings->sync();
        } else {
            state = -2;
        }
//        qDebug() << isSuccess << "restoreDefaultConfig===========>" << settingPath << defaultPath;

        return state;
    } else {
        return state;
    }
}


/*******************************************
  功能：是否为主设备
  参数：
  返回值：1,是主设备    0,为副设备
  ******************************************/
int Configclass::IsMasterDev() const
{
    QString key = "SYSTEM/IsMasterDev";
    int value = settings->value(key).toInt();
    return value;
}

void Configclass::setMasterDev(int isMasterDev)
{
    QString key = "SYSTEM/IsMasterDev";
    settings->setValue(key, isMasterDev);
}

//QString Configclass::getMaterIp() const
//{
//    QString key = "SYSTEM/MasterDevIP";
//    QString value = settings->value(key).toString();
//    return value;
//}

int Configclass::showScreenIndex() const
{
    QString key = "SYSTEM/ScreenIndex";
    int index = settings->value(key).toInt()-1;

    qDebug() << "index:" << index;
    if (index < 1)
        index = 0;

    return index;
}

QString Configclass::getSysCom() const
{
    QString key = "SYSTEM/com";
    QString value = settings->value(key).toString();
    return value;
}

int Configclass::getSysBaud() const
{
    QString key = "SYSTEM/baud";
    int value = settings->value(key).toInt();
    return value;
}

int Configclass::getReplortState() const
{
    QString key = "SYSTEM/ReportSlcdState";
    int value = settings->value(key).toInt();
    return value;
}

QString Configclass::getSysFont() const
{
    QString key = "SYSTEM/font";
    QString value = settings->value(key).toString();
    return value;
}

//日志保留天数
int Configclass::getSysReserveDays() const
{
    QString key = "SYSTEM/LogReserveDays";
    int value = settings->value(key).toInt();
    return value;
}

/*******************************************
  功能：得到播放本地信息时间
  参数：
  返回值：返回版本号
  ******************************************/
int Configclass::getBroadcastInfoTime() const
{
    QString key = "SYSTEM/BroadcastInfoTime";
    int value = settings->value(key).toInt();
    return value;
}

/*******************************************
  功能：得到当前Slcd版本
  参数：
  返回值：返回版本号
  ******************************************/
QString Configclass::getSlcdVersion() const
{
    QString key = "SYSTEM/SLCD_Version";
    QString value = settings->value(key).toString();
    return value;
}

/*******************************************
  功能：得到当前播放列表项目名
  参数：
  返回值：返回版本号
  ******************************************/
QString Configclass::getScheduleProName() const
{
    QString key = "SCHEDULE/project";
    QString value = settings->value(key).toString();
    return value;
}

/*******************************************
  功能：得到当前播放列表版本号
  参数：
  返回值：返回版本号
  ******************************************/
QString Configclass::getScheduleVersion() const
{
    QString key = "SCHEDULE/version";
    QString value = settings->value(key).toString();
    return value;
}

/*******************************************
  功能：得到播放列表档案
  参数：
  返回值：返回版本号
  ******************************************/
qint64 Configclass::getResourceSize() const
{
    QString key = "SCHEDULE/size";
    qint64 value = settings->value(key).toInt();

    return value;
}

/*******************************************
  功能：得到绑定的端口号
  参数：
  返回值：返回端口号
  ******************************************/
QString Configclass::getConfigPort()
{
    QString key = "SYSTEM/TcpPort";
    QString value = settings->value(key).toString();
    return value;
}

QString Configclass::getUpdatePort() const
{
    QString key = "SYSTEM/UpdatePort";
    QString value = settings->value(key).toString();
    return value;
}

/*******************************************
  功能：得到绑定的IP
  参数：
  返回值：IP地址
  ******************************************/
QString Configclass::getBindIP()
{
    QString key = "SYSTEM/IP";
    QString value = settings->value(key).toString();
    return value;
}


/*******************************************
  功能：得到当前车厢号
  参数：
  返回值：车厢号
  ******************************************/
int Configclass::getCarNum() const
{
    QString key = "POSITION/car";
    int value = settings->value(key).toInt();
    return value;
}

/*******************************************
  功能：得到当前位置号
  参数：
  返回值：
  ******************************************/
int Configclass::getCarSide() const
{
    QString key = "POSITION/side";
    int value = settings->value(key).toInt();
    return value;
}

QString Configclass::getCarSideStr() const
{
    QString key = "POSITION/side";
    QString value = settings->value(key).toString();
    return value;
}

QString Configclass::getTISCom() const
{
    QString key = "TISCOM/com";
    QString value = settings->value(key).toString();
    return value;
}

int Configclass::getTISBaud() const
{
    QString key = "TISCOM/baud";
    int value = settings->value(key).toInt();
    return value;
}

QStringList Configclass::getFsmList() const
{
    QString key = "TISCOM/fsm";
    QStringList value = settings->value(key).toString().split(",");
    return value;
}

/*******************************************
  功能：得到系统日期
  参数：
  返回值：
  ******************************************/
QString Configclass::getSystemDate() const
{
    QString key = "SYSTEM/systemDate";
    QString value = settings->value(key).toString();
    return value;
}

/*******************************************
  功能：得到系统时间
  参数：
  返回值：
  ******************************************/
QString Configclass::getSystemTime() const
{
    QString key = "SYSTEM/systemTime";
    QString value = settings->value(key).toString();
    return value;
}

/*******************************************
  功能：得到CPU安全界限
  参数：
  返回值：
  ******************************************/
int Configclass::getCputLimmit() const
{
    QString key = "SAFE/CpuLimmit";
    int value = settings->value(key).toInt();
    return value;
}

/*******************************************
  功能：得到内存安全界限
  参数：
  返回值：
  ******************************************/
int Configclass::getMemoryLimmit() const
{
    QString key = "SAFE/MemoryLimmit";
    int value = settings->value(key).toInt();
    return value;
}

/*******************************************
  功能：得到硬盘安全界限
  参数：
  返回值：
  ******************************************/
int Configclass::getHardDiskLimmit() const
{
    QString key = "SAFE/HardDiskLimmit";
    int value = settings->value(key).toInt();
    return value;
}

/*******************************************
  功能：得到LCDList
  参数：
  返回值：
  ******************************************/
// QStringList Configclass::getLCDIDStrList()
//{
//     QStringList IDList;
//     this->init();
//     for(int i = 1; i < 7; i++)
//     {
//         QString key = QObject::tr("SLCD/SLCD%1").arg(i);
//         QString value = settings->value(key).toString();
//         if (value != "0")
//         {
//             IDList.append(value);
//         }
//     }
//     return IDList;
//}

 /*******************************************
   功能：得到LCDList
   参数：
   返回值：
   ******************************************/
  QList<PanelInfoStruct> Configclass::getSLCDInfoIntList()
 {
      QList<PanelInfoStruct> infoList;

      this->init();
      for(int i = 1; i < 7; i++)
      {
          PanelInfoStruct infoStruct;
          QString key = QObject::tr("SLCD/SLCD%1").arg(i);
          QString value = settings->value(key).toString();

          QStringList list = value.split(":");

          if (list.count() == 2)
          {
              if (list.at(0) != 0)
              {
                  bool isFind = false;
                  //如果SLCD的ID有相同的，那么只算一个
                  for(int j = 0; j < infoList.count(); j++)
                  {
                      if (list.at(0).toInt() == infoList.at(j).panelID)
                      {
                          isFind = true;
                      }
                  }

                  if (!isFind)
                  {
                      infoStruct.panelID = list.at(0).toInt();
                      infoStruct.isCollect = list.at(1).toInt();
                      for(i = 0; i < infoList.count(); i++)
                      {
                          if (infoStruct.panelID < infoList.at(i).panelID)
                          {
                              infoList.insert(i, infoStruct);
                              break;
                          }
                      }
                      if (i == infoList.count())
                          infoList.append(infoStruct);
                  }
              }
          }

      }
      return infoList;
 }

  int Configclass::collectSLCDCount()
  {
      this->init();
      int collectCount = 0;

      for(int i = 1; i < SLCD_MAX_COUNT; i++)
      {
          PanelInfoStruct infoStruct;
          QString key = QObject::tr("SLCD/SLCD%1").arg(i);
          QString value = settings->value(key).toString();

          QStringList list = value.split(":");

          if (list.count() == 2)
          {
              if (list.at(0) != 0)
              {
                  infoStruct.panelID = list.at(0).toInt();
                  infoStruct.isCollect = list.at(1).toInt();
                  if (infoStruct.isCollect > 0)
                  {
                      collectCount++;
                  }
              }
          }

      }
      return collectCount;
  }

  PanelInfoStruct Configclass::getSLCDInfo(const int num)
  {
      this->init();

      PanelInfoStruct infoStruct;
      infoStruct.panelID = 0;
      infoStruct.isCollect = 0;

      QString key = QObject::tr("SLCD/SLCD%1").arg(num);
      QString value = settings->value(key).toString();
      QStringList list = value.split(":");
      if (list.count() == 2)
      {
          if (list.at(0) != 0)
          {
              infoStruct.panelID = list.at(0).toInt();
              infoStruct.isCollect = list.at(1).toInt();
          }
      }
      return infoStruct;
  }

/*******************************************
  功能：得到LCD
  参数：
  返回值：
  ******************************************/
QString Configclass::getLCDID() const
{
    QString key = "SLCD/ID";
    QString value = settings->value(key).toString();
    return value;
}


/*******************************************
  功能：得到LCD亮度
  参数：
  返回值：
  ******************************************/
int Configclass::getLcdDefaultLV() const
{
    QString key = "SLCD/DefaultLV";
    int defaultLV = settings->value(key).toInt();

    if (defaultLV > 4)
    {
        defaultLV = 4;
    } else if (defaultLV <= 0)
    {
        defaultLV = 1;
    }
    return defaultLV;
}

/*******************************************
  功能：得到LCD对比度
  参数：
  返回值：
  ******************************************/
int Configclass::getLCDCcontrast() const
{
    QString key = "SLCD/contrast";
    int value = settings->value(key).toInt();
    return value;
}

/*******************************************
  功能：得到LCD颜色(格式为red:green:blue,100:110:120)
  参数：
  返回值：
  ******************************************/
QString Configclass::getLCDColor() const
{
    QString key = "SLCD/color";
    QString value = settings->value(key).toString();
    return value;
}

/*******************************************
  功能：得到LCD 转换值
  参数：
  返回值：
  ******************************************/
int Configclass::getLcdTransformValue() const
{
    QString key = "SLCD/transformValue";
    int value = settings->value(key).toInt();
    return value;
}

/*******************************************
  功能：得到检测环境光度COM口
  参数：
  返回值：
  ******************************************/
QString Configclass::getPhotoSensorCom() const
{
    QString key = "PhotoSensor/com";
    QString value = settings->value(key).toString();
    return value;
}

int Configclass::getPhotoSensorBaud() const
{
    QString key = "PhotoSensor/baud";
    int value = settings->value(key).toInt();
    return value;
}

int Configclass::getIsCollectPS()
{
    QString key = "PhotoSensor/IsCollectPS";
    int value = settings->value(key).toInt();
    return value;
}

void Configclass::setSlcdVersion(const QString &version) const
{
    QString key = "SYSTEM/SLCD_Version";
    QString strVersion = QObject::tr("%1-%2").arg(SLCD_VERSION_TYPE).arg(version);
    settings->setValue(key, strVersion);
}


/*******************************************
  功能：得到LCD网络视频COM口
  参数：
  返回值：
  ******************************************/
QString Configclass::getLcdCom() const
{
    QString key = "SLCD/com";
    QString value = settings->value(key).toString();
    return  value;
}

int Configclass::getLcdBaud() const
{
    QString key = "SLCD/baud";
    int value = settings->value(key).toInt();
    return value;
}

/*******************************************
  功能：得到滚动文字的滚动速度
  参数：
  返回值：
  ******************************************/
int Configclass::getScrollSpeed() const
{
    QString key = "ScrollTxt/ScrollSpeed";
    int value = settings->value(key).toInt();
    return value;
}

/*******************************************
  功能：得到环境光度转化为LCD亮度的K值
  参数：
  返回值：环境光度转化为LCD亮度的K值
  ******************************************/
double Configclass::getPhoToBrightK() const
{
    QString key = "SLCD/K";
    double value = settings->value(key).toDouble();
//    qDebug() << "LCD/k===========>" << value;
    return  value;
}

/*******************************************
  功能：得到是否发送的为ascii码值
  参数：
  返回值：
  ******************************************/
bool Configclass::getSendIsAscii() const
{
    QString key = "SLCD/sendIsAscii";
    bool value = settings->value(key).toBool();
    return value;
}

/*******************************************
  功能：取得LCD亮度控制,分为4个等级,level1~level4
  参数：start,环境亮度开始值;start,环境亮度结束值
  返回值：LCD亮度;
  ******************************************/
LcdBriLVStruct Configclass::getLv1Bright() const
{
    LcdBriLVStruct lvStruct;
    QString key = "SLCD/level1";
    QString value = settings->value(key).toString();
    if (value == "")
    {
        key = "level1";
        value = settings->value(key).toString();
    }
    if (value == "")
    {
        value = "0-0:0";
    }
    QStringList list1 = value.split("-");
    QStringList list2 = list1.at(1).split(":");

    lvStruct.envirStart = list1.at(0).toInt();
    lvStruct.envirEnd = list2.at(0).toInt();
    lvStruct.lcdBirght = list2.at(1).toInt();
    return lvStruct;
}

LcdBriLVStruct Configclass::getLv2Bright() const
{
    LcdBriLVStruct lvStruct;

    QString key = "SLCD/level2";
    QString value = settings->value(key).toString();
    if (value == "")
    {
        key = "level2";
        value = settings->value(key).toString();
    }
    if (value == "")
    {
        value = "0-0:0";
    }
    QStringList list1 = value.split("-");
    QStringList list2 = list1.at(1).split(":");

    lvStruct.envirStart = list1.at(0).toInt();
    lvStruct.envirEnd = list2.at(0).toInt();
    lvStruct.lcdBirght = list2.at(1).toInt();
    return lvStruct;
}

LcdBriLVStruct Configclass::getLv3Bright() const
{
    LcdBriLVStruct lvStruct;

    QString key = "SLCD/level3";
    QString value = settings->value(key).toString();
    if (value == "")
    {
        key = "level3";
        value = settings->value(key).toString();
    }
    if (value == "")
    {
        value = "0-0:0";
    }
    QStringList list1 = value.split("-");
    QStringList list2 = list1.at(1).split(":");

    lvStruct.envirStart = list1.at(0).toInt();
    lvStruct.envirEnd = list2.at(0).toInt();
    lvStruct.lcdBirght = list2.at(1).toInt();
    return lvStruct;
}

LcdBriLVStruct Configclass::getLv4Bright() const
{
    LcdBriLVStruct lvStruct;

    QString key = "SLCD/level4";
    QString value = settings->value(key).toString();
    if (value == "")
    {
        key = "level4";
        value = settings->value(key).toString();
    }
    if (value == "")
    {
        value = "0-0:0";
    }
    QStringList list1 = value.split("-");
    QStringList list2 = list1.at(1).split(":");

    lvStruct.envirStart = list1.at(0).toInt();
    lvStruct.envirEnd = list2.at(0).toInt();
    lvStruct.lcdBirght = list2.at(1).toInt();
    return lvStruct;
}



int Configclass::getAckCEPort()
{
    QString key = "SYSTEM/AckCePort";
    int value = settings->value(key).toInt();
    return value;
}

int Configclass::getReceCEPort()
{
    QString key = "SYSTEM/ReceCEPort";
    int value = settings->value(key).toInt();
    return value;
}

//得到是否打印串口信息
bool Configclass::getDebugSerialPort()
{
    QString key = "DEBUG/SerialPort";
    bool value = false;
    value = settings->value(key).toBool();
    return value;
}

 //是否打印TIS处理信息
bool Configclass::getDebugTisCtl()
{
    QString key = "DEBUG/TisCtl";
    bool value = false;
    value = settings->value(key).toBool();
    return value;
}

//------------------------- 设置接口 --------------------------//

/*******************************************
  功能：设置当前播放列表项目名
  参数：
  返回值：
  ******************************************/
void Configclass::setScheduleProName(const QString& proName) const
{
    QString key = "SCHEDULE/project";
    settings->setValue(key, proName);
}

/*******************************************
  功能：设置当前播放列表版本号
  参数：
  返回值：
  ******************************************/
void Configclass::setScheduleVersion(const QString &version)
{
    QString key = "SCHEDULE/version";
    settings->setValue(key, version);
}

/*******************************************
  功能：设置播放列表资源大小
  参数：
  返回值：
  ******************************************/
void Configclass::setResourceSize(const qint64 size) const
{
    QString key = "SCHEDULE/size";
    settings->setValue(key, size);
}

/*******************************************
  功能：设置绑定的端口号
  参数：
  返回值：返回端口号
  ******************************************/
void Configclass::setConfigPort(const int port)
{
    QString key = "SYSTEM/TcpPort";
    settings->setValue(key, port);
}

/*******************************************
  功能：设置绑定的IP
  参数：
  返回值：IP地址
  ******************************************/
void Configclass::setBindIP(const QString &ip)
{
    QString key = "SYSTEM/IP";
    settings->setValue(key, ip);
}


/*******************************************
  功能：设置当前车厢号
  参数：
  返回值：车厢号
  ******************************************/
void Configclass::setCarNum(const int carrageNum) const
{
    QString key = "POSITION/car";
    settings->setValue(key, carrageNum);
}

/*******************************************
  功能：设置当前位置号
  参数：
  返回值：
  ******************************************/
void Configclass::setCarSide(const int position) const
{
    QString key = "POSITION/side";
    settings->setValue(key, position);
}

/*******************************************
  功能：设置系统日期
  参数：
  返回值：
  ******************************************/
void Configclass::setSystemDate(const QString &date) const
{
    QString key = "SYSTEM/systemDate";
    settings->setValue(key, date);
}

/*******************************************
  功能：设置系统时间
  参数：
  返回值：
  ******************************************/
void Configclass::setSystemTime(const QString &time) const
{
    QString key = "SYSTEM/systemTime";
    settings->setValue(key, time);
}

/*******************************************
  功能：设置CPU安全界限
  参数：
  返回值：
  ******************************************/
void Configclass::setCputLimmit(const int cpuLimmit) const
{
    QString key = "SAFE/CpuLimmit";
    settings->setValue(key, cpuLimmit);
}

/*******************************************
  功能：设置内存安全界限
  参数：
  返回值：
  ******************************************/
void Configclass::setMemoryLimmit(const int memoryLimmit) const
{
    QString key = "SAFE/MemoryLimmit";
    settings->setValue(key, memoryLimmit);
}

/*******************************************
  功能：设置硬盘安全界限
  参数：
  返回值：
  ******************************************/
void Configclass::setHardDiskLimmit(const int hardDiskLimmit) const
{
    QString key = "SAFE/HardDiskLimmit";
    settings->setValue(key, hardDiskLimmit);
}
/*******************************************
  功能：设置LCD ID号
  参数：
  返回值：
  ******************************************/
void Configclass::setLCDID(const QString &ID) const
{

    QString key = "SLCD/ID";
    settings->setValue(key, ID);
}

/*******************************************
  功能：设置LCDList
  参数：
  返回值：
  ******************************************/
void Configclass::setLCDIDList(const QStringList& IDList) const
{
    QString key = "SLCD/ID";
    QString ID;
    QStringList list = IDList;
    list.removeDuplicates();
    list.sort();
    for(int i = 0; i < list.count(); i++)
    {
        ID.append(IDList.at(i));
        ID.append("/");
    }
    ID.remove(ID.count() - 1);


    settings->setValue(key, ID);
}

/*******************************************
  功能：设置LCD亮度
  参数：
  返回值：
  ******************************************/
void Configclass::setLcdDefaultLV(const int lv) const
{
    QString key = "SLCD/DefaultLV";

    int defaultLv = lv;
    if (defaultLv > 4)
    {
        defaultLv = 4;
    } else if (defaultLv <= 0)
    {
        defaultLv = 1;
    }

    settings->setValue(key, defaultLv);
}

/*******************************************
  功能：设置LCD对比度
  参数：
  返回值：
  ******************************************/
void Configclass::setLCDCcontrast(const int contrast) const
{
    QString key = "SLCD/contrast";
    settings->setValue(key, contrast);
}

/*******************************************
  功能：设置LCD颜色(格式为red:green:blue,100:110:120)
  参数：
  返回值：
  ******************************************/
void Configclass::setLCDColor(const QString &color) const
{
    QString key = "SLCD/color";
    settings->setValue(key, color);
}

/*******************************************
  功能：设置环境光度转化为LCD亮度的增值
  参数：
  返回值：
  ******************************************/
void Configclass::setLcdTransformValue(const int transformValue) const
{
    QString key = "SLCD/transformValue";
    settings->setValue(key, transformValue);
}

/*******************************************
  功能：设置检测环境光度COM口
  参数：
  返回值：
  ******************************************/
void Configclass::setEnvirCom(const QString &com) const
{
    QString key = "PhotoSensor/com";
    settings->setValue(key, com);
}

/*******************************************
  功能：设置LCD网络视频COM口
  参数：
  返回值：
  ******************************************/
void Configclass::setLcdCom(const QString &com) const
{
    QString key = "SLCD/com";
    settings->setValue(key, com);
}

/*******************************************
  功能：设置滚动文字的滚动速度
  参数：
  返回值：
  ******************************************/
void Configclass::setScrollSpeed(const int speed) const
{
    QString key = "ScrollTxt/ScrollSpeed";
    settings->setValue(key, speed);

}


/*******************************************
  功能：设置LCD亮度控制,分为4个等级,level1~level4
  参数：bright,LCD亮度;start,环境亮度开始值;start,环境亮度结束值
  返回值：
  ******************************************/
void Configclass::setLv1Bright(const LcdBriLVStruct &lvStruct)
{
    QString key = "SLCD/level1";
    QString vaule = QObject::tr("%1-%2:%3").arg(lvStruct.envirStart).arg(lvStruct.envirEnd).arg(lvStruct.lcdBirght);
    settings->setValue(key, vaule);
}

void Configclass::setLv2Bright(const LcdBriLVStruct &lvStruct)
{
    QString key = "SLCD/level2";
    QString vaule = QObject::tr("%1-%2:%3").arg(lvStruct.envirStart).arg(lvStruct.envirEnd).arg(lvStruct.lcdBirght);
    settings->setValue(key, vaule);
}

void Configclass::setLv3Bright(const LcdBriLVStruct &lvStruct)
{
    QString key = "SLCD/level3";
    QString vaule = QObject::tr("%1-%2:%3").arg(lvStruct.envirStart).arg(lvStruct.envirEnd).arg(lvStruct.lcdBirght);
    settings->setValue(key, vaule);
}

void Configclass::setLv4Bright(const LcdBriLVStruct &lvStruct)
{
    QString key = "SLCD/level4";
    QString vaule = QObject::tr("%1-%2:%3").arg(lvStruct.envirStart).arg(lvStruct.envirEnd).arg(lvStruct.lcdBirght);
    settings->setValue(key, vaule);
}

void Configclass::delLCDID(const QString& ID) const
{
    QString key = "SLCD/ID";
    QString strID = this->getLCDID();
    QString id = ID;

    if (strID.contains(id))
    {
        QString id1 = id + "/";
        if (strID.contains(id1))
        {
            strID = strID.remove(id1);
        } else {
            strID = strID.remove(id);
        }
    }
    settings->setValue(key, strID);
}

void Configclass::setSlcdInfo(const QList<PanelInfoStruct>& infoList)
{
    QList<PanelInfoStruct> localInfoList = this->getSLCDInfoIntList();
    for(int i = 0; i < infoList.count(); i++)
    {
//        qDebug() << i << "i=>" << infoList.at(i).panelID;
        for(int j = 0; j < localInfoList.count(); j++)
        {
            if (localInfoList.at(j).panelID == infoList.at(i).panelID)
            {
                QString key = QObject::tr("SLCD/SLCD%1").arg(j+1);
                QString strValue = QObject::tr("%1:%2").arg(infoList.at(i).panelID).arg(infoList.at(i).isCollect);
                settings->setValue(key, strValue);
                break;
            }
        }
    }
}

void Configclass::addLCDIDList(const QStringList& IDList) const
{
    QString key = "SLCD/ID";
    QString strID = this->getLCDID();



    for(int i = 0; i < IDList.count(); i++)
    {

        if (strID.contains(IDList.at(i)))
        {
            continue;
        }
        strID.append("/");
        strID.append(IDList.at(i));
    }

    settings->setValue(key, strID);
}


void Configclass::addLCDID(const QString& ID) const
{
    QString key = "SLCD/ID";
    QString strID = this->getLCDID();

    if (!strID.contains(ID))
    {
        strID.append("/");
        strID.append(ID);
        settings->setValue(key, strID);
    }
}

void Configclass::delLCDIDList(const QStringList& IDList) const
{
    QString key = "SLCD/ID";
    QString strID = this->getLCDID();

//    for(int i = 0; i < IDList.count(); i++)
//    {
//        if (i != 0)
//        {
//            strID.append("/");
//        }
//        strID.append(IDList.at(i));
//    }

    int index;
    for(int i = 0; i < IDList.count(); i++)
    {
        QString id = IDList.at(i);

        if (strID.contains(id))
        {
            QString id1 = id + "/";
            if (strID.contains(id1))
            {
                index = strID.indexOf(id1);
                strID = strID.remove(id1);
            } else {
                id1 = "/" + id;
                strID = strID.remove(id1);
            }
        }
    }
    settings->setValue(key, strID);
}


//-----------------------------------------------------//

/*******************************************
  功能：设置一个配置信息的值
  参数：group，配置的分组及它的名字，格式:类别/属性 比如”state/position“
     strConfiguration QString类型配置值
  ******************************************/
void Configclass::setConfigValueStr(const QString &group, const QString &strValue)
{

    settings->setValue(group, strValue);
}

/*******************************************
  功能：设置一个配置信息的值
  参数：group，配置的分组及它的名字,格式:类别/属性,比如”state/position“
     iValue int类型配置值
  ******************************************/
void Configclass::setConfigValueInt(const QString &group, int iValue)
{
    settings->setValue(group, iValue);
}

/*******************************************
  功能：   取得一个配置信息的值
  参数：   group，配置的分组及它的名字，比如”state/position“
  返回值：  返回一个QString类型的配置值
  ******************************************/
QString Configclass::getConfigValueStr(const QString &group) const
{
    QString strValue = settings->value(group).toString();
    return strValue;
}

/*******************************************
  功能:   取得一个配置信息的值
  参数:   group，配置的分组及它的名字，比如”state/position“
  返回值:  返回一个int类型的配置值
  ******************************************/
int Configclass::getConfigValueInt(const QString &group) const
{
    int iValue = settings->value(group).toInt();

    return iValue;
}


/*******************************************
  功能:   取得一个配置文件内所有配置信息的值
  参数:
  返回值:  返回一个QMap
  ******************************************/
QMap<QString, int>  Configclass::getAllConfigMap()
{
    QMap<QString, int> configMap;

    //遍历每一个分组中的每一个配置和它们的值
    QStringList list = settings->childGroups();
    for(int i = 0; i < list.count(); i++)
    {
        settings->beginGroup(list.at(i));
        QStringList keyList = settings->childKeys();

        for (int j = 0; j < keyList.count(); j++)
        {
            QString key = keyList.at(j);

            //因为现在遍历每个小组，所以记得直接用它们的属性名，不用加组名：
            //如state/position state:组名 position:属性名
            int value = this->getConfigValueInt(key);

            //查找完值后将组名重新加上
            key = list.at(i) + "/" + key;
            configMap.insert(key, value);
        }
        settings->endGroup();
    }
    return configMap;
}

/*******************************************
  功能:   取得一个配置文件内所有配置信息的值
  参数:
  返回值:  返回一个配置文件结构体ConfigStruct
  ******************************************/
ConfigStruct Configclass::getConfigStruct() const
{
//    qDebug() << "==================>> getConfigStruct <<====================";
    ConfigStruct configStruct;

    QStringList list = settings->childGroups();

    for(int i = 0; i < list.count(); i++)
    {
//        qDebug() << list.count() << "i=====================>"  << i << list.at(i);
        settings->beginGroup(list.at(i));
        QStringList keyList = settings->childKeys();

        for (int j = 0; j < keyList.count(); j++)
        {
            QString key = keyList.at(j);

            //因为现在遍历每个小组，所以记得直接用它们的属性名，不用加组名：
            //如state/position state:组名 position:属性名

            if (key == "DefaultLV")
            {
                int value = this->getConfigValueInt(key);
                configStruct.defaultLv = value;
            } else if (key == "level1")
            {
                configStruct.lvStruct[0] = this->getLv1Bright();
            } else if (key == "level2")
            {
                configStruct.lvStruct[1] = this->getLv2Bright();
            } else if (key == "level3")
            {
                configStruct.lvStruct[2] = this->getLv3Bright();
            } else if (key == "level4")
            {
                configStruct.lvStruct[3] = this->getLv4Bright();
            } else if (key == "car")
            {
                int value = this->getConfigValueInt(key);
                configStruct.carNum = value;
            } else if (key == "side")
            {
                QString value = this->getConfigValueStr(key);

//                if (value == "A" || value == "a")
                if (value == "R" || value == "r")
                {
                    configStruct.carSide = 65;
                } else {
                    configStruct.carSide = 66;
                }
            } else if (key == "systemDate")
            {
                configStruct.systemDate = QDate::currentDate();
            } else if (key == "systemTime")
            {
                configStruct.systemTime = QTime::currentTime();
            } else if (key == "ScrollSpeed")
            {
                int value = this->getConfigValueInt(key);
                configStruct.scrollSpeed = value;
            } else if (key == "CpuLimmit")
            {
                int value = this->getConfigValueInt(key);
                configStruct.cpuLimmit = value;
            } else if (key == "MemoryLimmit")
            {
                int value = this->getConfigValueInt(key);
                configStruct.memoryLimmit = value;
            } else if (key == "HardDiskLimmit")
            {
                int value = this->getConfigValueInt(key);
                configStruct.hardDiskLimmit = value;
            } else if (key == "limmit")
            {
                int value = this->getConfigValueInt(key);
                configStruct.limmit = value;
            } else if (key == "ReportSlcdState")
            {
                int value = this->getConfigValueInt(key);
                configStruct.isReplortSlcd = value;
            } else if (key == "project")
            {
                QString value = this->getConfigValueStr(key);
                configStruct.versionInfo.schedulePro = value;
            } else if (key == "size")
            {
//                int value = this->getConfigValueInt(key);
                qint64 value = settings->value(key).toLongLong();
                configStruct.versionInfo.resourceSize = value;
            } else if (key == "SLCD_Version")
            {
                QString value = this->getConfigValueStr(key);

                if (value.contains("beta-"))
                {
                    configStruct.versionInfo.enVersionType = VERSION_BTEA;
                    value = value.remove("beta-");
                } else if (value.contains("release-"))
                {
                    configStruct.versionInfo.enVersionType = VERSION_RELEASE;
                    value = value.remove("release-");
                } else {
                    value = SLCD_VERSION_TYPE;
                    if (value == "release")
                    {
                        configStruct.versionInfo.enVersionType = VERSION_RELEASE;
                    } else {
                        configStruct.versionInfo.enVersionType = VERSION_BTEA;
                    }
                    value = SLCD_VERSION_STR;
                }

                QStringList list = value.split(".");

                if (list.count() >= 2)
                {
                    configStruct.versionInfo.version1 = list.at(0).toInt();
                    configStruct.versionInfo.version2 = list.at(1).toInt();
                    configStruct.versionInfo.version3 = list.at(2).toInt();
                }

            } else if (key == "IsMasterDev")
            {
                int value = this->getConfigValueInt(key);
                configStruct.isMastDev = value;
            } else if (key == "BroadcastInfoTime")
            {
                int value = this->getConfigValueInt(key);
                configStruct.bcInfoTime = value;
            } else if (key == "IsCollectPS")
            {
                int value = this->getConfigValueInt(key);
                configStruct.isCollectPS = value;
            }


            //            else if (key == "IsMasterDev")
            //            {
            //                int value = this->getConfigValueInt(key);
            //                configStruct.transformValue = value;
            //            } else if (key == "contrast")
            //            {
            //                int value = this->getConfigValueInt(key);
            //                configStruct.contrast = value;
            //            } else if (key == "color")
            //            {
            //                QString value = this->getConfigValueStr(key);
            //                configStruct.color = value;
            //            }
        }
        settings->endGroup();
    }


    configStruct.systemTime = QTime::currentTime();
    configStruct.systemDate = QDate::currentDate();

//    qDebug() << configStruct.cpuLimmit << configStruct.memoryLimmit << configStruct.hardDiskLimmit;
    return configStruct;
}


/*******************************************
  功能:   设置配置信息的值
  参数:   configStruct 配置信息结构体
  返回值:
  ******************************************/
bool Configclass::setConfig(const ConfigStruct& configStruct)
{
    QStringList configNameList;
    ConfigStruct configStru = configStruct;
    int iValue;
    QString strValue;

    configNameList << "SLCD/DefaultLV"          << "SLCD/level1"             << "SLCD/level2"                << "SLCD/level3"               << "SLCD/level4" \
                   << "POSITION/car"            << "POSITION/side"           << "SYSTEM/IsMasterDev"         << "SYSTEM/MasterDevIP"   \
                   << "SYSTEM/ReportSlcdState"  << "SYSTEM/LogReserveDays"   << "SYSTEM/BroadcastInfoTime"   << "PhotoSensor/IsCollectPS"
                   << "ScrollTxt/ScrollSpeed"   << "SAFE/CpuLimmit"          << "SAFE/MemoryLimmit"          << "SAFE/HardDiskLimmit"        << "SAFE/limmit";

    if (!settings->isWritable())
    {
        settings->setPath(QSettings::IniFormat, QSettings::UserScope, g_installPath + "/config.ini");
    }

#ifdef _TTY_POSIX_

    //设置系统时间
//    QString dateStr = QObject::tr("date -s %1").arg(configStruct.systemDate.toString("MM/dd/yyyy"));
//    QString timeStr = QObject::tr("date -s %1").arg(configStruct.systemTime.toString("hh:mm:ss"));


//    system(dateStr.toLatin1().data());
//    system(timeStr.toLatin1().data());
//    system("hwclock -w");

//    system("hwclock -s");
//    setLinuxSysDataTime(configStruct.systemDate.year(), configStruct.systemDate.month(), configStruct.systemDate.day(),
//                        configStruct.systemTime.hour(), configStruct.systemTime.minute(), configStruct.systemTime.second());

#endif

    for(int i = 0; i < configNameList.count(); i++)
    {
        QString configName = configNameList.at(i);

        if (configName.contains("DefaultLV"))
        {
            iValue = configStru.defaultLv;
            settings->setValue(configName, iValue);
        } else if (configName.contains("level1"))
        {
            this->setLv1Bright(configStru.lvStruct[0]);
        } else if (configName.contains("level2"))
        {
            this->setLv2Bright(configStru.lvStruct[1]);
        } else if (configName.contains("level3"))
        {
            this->setLv3Bright(configStru.lvStruct[2]);
        } else if (configName.contains("level4"))
        {
            this->setLv4Bright(configStru.lvStruct[3]);
        } else if (configName.contains("car"))
        {
            iValue = configStru.carNum;
            settings->setValue(configName, iValue);
        } else if (configName.contains("side"))
        {
            if (configStru.carSide == 65 || configStru.carSide == 97)
            {
//                strValue = "A";
                strValue = "R";
            } else {
//                strValue = "B";
                strValue = "L";
            }

            settings->setValue(configName, strValue);
        } else if (configName.contains("systemDate"))
        {
            strValue = configStru.systemDate.toString("yyyy.MM.dd");
            settings->setValue(configName, strValue);
        } else if (configName.contains("systemTime"))
        {
            strValue = configStru.systemTime.toString("h:m:s");
            settings->setValue(configName, strValue);
        } else if (configName.contains("IsMasterDev"))
        {
            if (configStru.isMastDev > 0)
            {
                iValue = configStru.isMastDev;
                settings->setValue(configName, iValue);
            }
        }  else if (configName.contains("MasterDevIP"))
        {
            if (configStru.masterDevIp != "")
            {
                strValue = configStru.masterDevIp;
                settings->setValue(configName, strValue);
            }
        }  else if (configName.contains("ReportSlcdState"))
        {
            if (configStru.isReplortSlcd != -1)
            {
                iValue = configStru.isReplortSlcd;
                settings->setValue(configName, iValue);
            }
        }  else if (configName.contains("LogReserveDays"))
        {
            if (configStru.logReserveDays != -1)
            {
                iValue = configStru.logReserveDays;
                settings->setValue(configName, iValue);
            }
        } else if (configName.contains("BroadcastInfoTime"))
        {
                iValue = configStru.bcInfoTime;
                settings->setValue(configName, iValue);
        } else if (configName.contains("IsCollectPS"))
        {
                iValue = configStru.isCollectPS;
                settings->setValue(configName, iValue);
        } else if (configName.contains("ScrollSpeed"))
        {           
                iValue = configStru.scrollSpeed;
                settings->setValue(configName, iValue);
        } else if (configName.contains("CpuLimmit"))
        {
            iValue = configStru.cpuLimmit;
            settings->setValue(configName, iValue);
        } else if (configName.contains("MemoryLimmit"))
        {
            iValue = configStru.memoryLimmit;
            settings->setValue(configName, iValue);
        } else if (configName.contains("HardDiskLimmit"))
        {
            iValue = configStru.hardDiskLimmit;
            settings->setValue(configName, iValue);
        } else if (configName.contains("limmit"))
        {
            iValue = configStru.limmit;
            settings->setValue(configName, iValue);
        }
//        else if (configName.contains("contrast"))
//        {
//            iValue = configStru.contrast;
//            settings->setValue(configName, iValue);
//        } else if (configName.contains("color"))
//        {
//            strValue = configStru.color;
//            settings->setValue(configName, strValue);
//        } else if (configName.contains("transformValue"))
//        {
//            iValue = configStru.transformValue;
//            settings->setValue(configName, iValue);
//        }
    }
    this->sync();
    return true;
}

void Configclass::sync()
{
    settings->sync();
}
